package com.rfid.afd.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rfid.afd.activity.adapter.SetLabAdapter;
import com.rfid.afd.activity.view.ISetLabView;
import com.rfid.afd.presenter.SetLabPresenter;
import com.rfid.rls_psw.R;
import com.rfid.util.AnimatorHelper;
import com.rfid.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
/**写标签*/
public class SetLabelActivity extends BaseActivity<SetLabPresenter> implements ISetLabView, View.OnClickListener,Handler.Callback {

    @BindView(R.id.write)
    Button mWrite;
    @BindView(R.id.read)
    Button mRead;

    @BindView(R.id.edit)
    EditText mEdit;
    @BindView(R.id.epc)
    TextView mTextView;
    @BindView(R.id.preEpc)
    TextView mPreEpcTextView;

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;

    private Handler mHandler;
    private List<LabBean> mLabBeans;
    private SetLabAdapter mLabAdapter;
    private LabBean mWriteBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_label);
        ButterKnife.bind(this);
        mWrite.setOnClickListener(this);
        mRead.setOnClickListener(this);
        mLabBeans = new ArrayList<>();

        mLabAdapter = new SetLabAdapter(this,mLabBeans);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRecyclerView.setAdapter(mLabAdapter);

        mHandler = new Handler(this);
    }

    @Override
    public SetLabPresenter getPresenter() {
        return new SetLabPresenter();
    }

    @Override
    public void onClick(View v) {
        if(v == mRead) {
            mPresenter.getEpc();
        } else if(v == mWrite) {
            String set = mEdit.getText().toString();
            if(!TextUtils.isEmpty(set)) {
                mWrite.setEnabled(false);
                mPresenter.writeEpc(set);
                mHandler.sendEmptyMessageDelayed(0,5000);
            } else {
                AnimatorHelper.shake(this,mEdit);
            }
        }
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void stopLoading() {
    }

    @Override
    public void onGetEpc(String epc) {
        LogUtils.v("onGetEpc = " + epc);
        mTextView.setText(epc);
    }

    @Override
    public void onWriteFail() {
        mHandler.removeMessages(0);
        Toast.makeText(this,"写入失败",Toast.LENGTH_SHORT).show();
        mWrite.setEnabled(true);
    }

    @Override
    public void onWrite(LabBean bean) {
        mHandler.removeMessages(0);
        mLabBeans.add(0,bean);
        mLabAdapter.notifyDataSetChanged();
        mWrite.setEnabled(true);
        Toast.makeText(this,"写入成功",Toast.LENGTH_SHORT).show();
        mEdit.setText("");
    }

    @Override
    public boolean handleMessage(@NonNull Message message) {
        System.out.println("handleMessage");
        mPresenter.getEpc();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mWrite.setEnabled(true);
        return false;
    }


    public static class LabBean{
        public String originEpc;
        public String barcode;
    }
}